package com.lazy.mongodemo.aggregation;

import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lazy.mongodemo.DemoQueryMtGps;
import com.mongodb.Block;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

/**
 * 对测试数据的抽象, 主要是对调用的mongoclient的简单封装
 * @author luodan
 *
 */
public class AbstractMongoClient {
    protected static final Logger logger = LoggerFactory.getLogger(DemoQueryMtGps.class);

    protected MongoClient client;
    protected MongoCollection<Document> coll;
    
    
    public AbstractMongoClient(String url, String dbName, String collectionName){
        // List<MongoCredential> credentials = new ArrayList<MongoCredential>();
        // credentials.add(MongoCredential.createCredential("sms_test", "ecodeSms", "sms_test".toCharArray()));
        //client = new MongoClient(new ServerAddress("127.0.0.1", 27017), credentials);
        //client = new MongoClient(host, port);
        MongoClientURI connectionString = new MongoClientURI(url);
        client = new MongoClient(connectionString);
 
        MongoDatabase db = client.getDatabase(dbName);
        coll = db.getCollection(collectionName);
    }
    
    protected static class PrintBlock implements Block<Document> {
        int count = 0;
        public PrintBlock(int count){
            this.count = count;
        }
        @Override
        public void apply(final Document document) {
            count++;
            // logger.info("{}", document.toJson());
        }
        
        public int getCount(){
            return count;
        }
    };
}
